<script setup>
import { onMounted, ref } from 'vue'

import CenterEarth from './components/centerEarth.vue'
import Evolution from './components/evolution.vue'
import Accelerate from './components/accelerate.vue'
// import WaterChart from './components/waterChart.vue'
import BottomChart from './components/bottomChart.vue'
import DialogForm from './components/dialogForm.vue'
// import TrainingLog from './components/TrainingLog.vue'
// import ResourceUsage from './components/ResourceUsage.vue'
import ModelPerformance from './components/ModelPerformance.vue'
import ResourceMonitor from './components/ResourceMonitor.vue'

// 维护所有loading的值
const uploadLoading = ref(false)
const startLoading = ref(false)
const evolutionLoading = ref(false)
const assessmentLoading = ref(false)
const issuedLoading = ref(false)
const updateLoading = ref(false)
const reasoningLoading = ref(false)
const sampleLoading = ref(false)

const loadingObj = {
  upload: uploadLoading,
  start: startLoading,
  evolution: evolutionLoading,
  assessment: assessmentLoading,
  issued: issuedLoading,
  update: updateLoading,
  reasoning: reasoningLoading,
  sample: sampleLoading
}

const changeLoading = (type, isLoading) => {
  loadingObj[type].value = isLoading
}

const w = ref()
const placeholderPic = () => {
  const myW = document.documentElement.clientWidth / 80
  w.value = myW
  document.documentElement.style.fontSize = w.value + 'px'
}
onMounted(() => {
  placeholderPic()
})

const currentLi = ref('')
const dialogVisible = ref(false)

const openDialog = (current) => {
  currentLi.value = current
  dialogVisible.value = true
}

const closeDialog = () => {
  dialogVisible.value = false
}

// 消息配置
const showMessage = (type, message) => {
  // 获取当前激活的 loading 元素
  const loadingElement = document.querySelector('.loading-icon')
  if (loadingElement) {
    const rect = loadingElement.getBoundingClientRect()

    ElMessage({
      dangerouslyUseHTMLString: true,
      message,
      type,
      duration: 5000,
      customClass: 'custom-message',
      style: {
        position: 'fixed',
        top: `${rect.top - 7}px`,
        left: `${rect.right + 25}px`, // loading图标右侧20px处
        transform: 'none'
      }
    })
  } else {
    // 如果没有找到loading元素，使用默认位置
    ElMessage({
      dangerouslyUseHTMLString: true,
      message,
      type,
      duration: 5000
    })
  }
}

// 添加 ref 以访问 BottomChart 组件
const bottomChartRef = ref(null)

// 添加处理任务启动的方法
const handleTaskStart = () => {
  // 确保 bottomChartRef 存在
  if (bottomChartRef.value) {
    bottomChartRef.value.startTaskLogs()
  }
}

const isTaskRunning = ref(false)

// 添加任务状态变更处理函数
const handleTaskStatusChange = (status) => {
  isTaskRunning.value = status
}
</script>

<template>
  <div class="container-data">
    <div class="header">
      <h1>
        <span>电力人工智能云边模型协同进化及推理加速</span>
      </h1>
    </div>
    <div class="main">
      <Evolution
        v-model:currentLi="currentLi"
        :openDialog="openDialog"
        :uploadLoading="uploadLoading"
        :startLoading="startLoading"
        :evolutionLoading="evolutionLoading"
        :assessmentLoading="assessmentLoading"
        :issuedLoading="issuedLoading"
        :changeLoading="changeLoading"
      />
      <!-- <ResourceUsage 
        class="resource-panel"
        :currentLi="currentLi"
        :strategyType="dialogVisible ? dialogForm?.strategyForm?.strategyType : ''"
        :subStrategyType="dialogVisible ? dialogForm?.strategyForm?.subStrategyType : ''"
        :taskRunning="isTaskRunning"
      /> -->
      <CenterEarth :currentLi="currentLi" />
      <ModelPerformance class="performance-panel" />
      <ResourceMonitor class="resource-panel" />
      <Accelerate
        :w="w"
        :currentLi="currentLi"
        :openDialog="openDialog"
        :updateLoading="updateLoading"
        :reasoningLoading="reasoningLoading"
        :sampleLoading="sampleLoading"
      />
      <!-- <TrainingLog /> -->
      <BottomChart ref="bottomChartRef" :taskType="currentLi" />
    </div>
  </div>
  <DialogForm
    :dialogVisible="dialogVisible"
    :closeDialog="closeDialog"
    :currentLi="currentLi"
    :showMessage="showMessage"
    :changeLoading="changeLoading"
    @start-task="handleTaskStart"
    @taskStatusChange="handleTaskStatusChange"
  />
</template>

<style lang="scss" scoped>
.header {
  height: 12%;
  width: 100%;
  background-color: #fff;
  background: url(./images/53titlebg.png) no-repeat top center;
  background-size: 100%;
  text-align: center;

  h1 {
    font-size: 1.5rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    line-height: 1.4;
    font-family: var(--font-family-base);
  }
}

.header h1 {
  font-size: 1.5rem;
  height: 100%;
  margin: auto;
  display: table;
  font-weight: 500;
  background-image: -webkit-linear-gradient(bottom, #86919e, #fff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.header h1 span {
  display: table-cell;
  vertical-align: middle;
}

.container-data {
  background: url(./images/53bg.png) no-repeat;
  background-size: 100%;
  width: 100vw;
  height: 100vh;
}

.main {
  width: 100%;
  height: 80%;
  padding: 0 2.5%;
  box-sizing: border-box;
  position: relative;
}

:deep(.highlight-number) {
  font-weight: 600;
  color: #f29701;
  font-size: 1.1em;
  font-family: var(--font-family-mono);
}

// 确保 Message 组件中的 HTML 样式生效
:deep(.el-message) {
  .el-message__content {
    font-size: 0.9rem;
    font-weight: 400;
    letter-spacing: var(--letter-spacing-base);
    line-height: var(--line-height-base);
  }
}

:global(.el-message) {
  position: fixed !important;
  margin: 0 !important;
  transform: none !important;
  background: rgba(16, 44, 58, 0.9) !important;
  border: 1px solid #3eacb4 !important;
  backdrop-filter: blur(8px);
  z-index: 9999;

  // 添加过渡动画
  animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

// 成功消息样式
:global(.el-message--success) {
  .el-message__content {
    color: #3eacb4 !important;
  }
  .el-message__icon {
    color: #3eacb4 !important;
  }
}

// 错误消息样式
:global(.el-message--error) {
  .el-message__content {
    color: #f29701 !important;
  }
  .el-message__icon {
    color: #f29701 !important;
  }
}

// 修改动画，根据消息框位置使用不同的动画
:global(.el-message.slide-from-left) {
  animation: slideFromLeft 0.3s ease-out;
}

:global(.el-message.slide-from-right) {
  animation: slideFromRight 0.3s ease-out;
}

@keyframes slideFromLeft {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes slideFromRight {
  from {
    opacity: 0;
    transform: translateX(20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.custom-message {
  min-width: 200px;
  padding: 10px 15px;
}

.resource-panel {
  position: absolute;
  right: 21%;
  top: 9%;
  width: 15%;
  height: 50%;
  background: linear-gradient(135deg, rgba(62, 172, 180, 0.05) 0%, rgba(9, 196, 202, 0.1) 100%);
  backdrop-filter: blur(5px);
  border: 1px solid rgba(62, 172, 180, 0.2);
  border-radius: 10px;
  overflow: hidden;
  box-shadow:
    0 0 20px rgba(62, 172, 180, 0.1),
    inset 0 0 20px rgba(62, 172, 180, 0.05);
  z-index: 1;
  padding: 15px;
}

.performance-panel {
  position: absolute;
  left: 19%;
  top: 9%;
  width: 15%;
  height: 50%;
  background: linear-gradient(135deg, rgba(62, 172, 180, 0.05) 0%, rgba(9, 196, 202, 0.1) 100%);
  backdrop-filter: blur(5px);
  border: 1px solid rgba(62, 172, 180, 0.2);
  border-radius: 10px;
  overflow: hidden;
  box-shadow:
    0 0 20px rgba(62, 172, 180, 0.1),
    inset 0 0 20px rgba(62, 172, 180, 0.05);
  z-index: 1;
  padding: 15px;
}
</style>
